package com.example.enterprisemanager.mvvm.fragment.manager;

import com.example.enterprisemanager.R;
import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.AllocateGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.Goods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.RuinGoods;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class RuinModel extends BaseModel {
    private Disposable getGoodDisposable;
    private Disposable updateIdDisposable;
    private Disposable updateGoodDisposable;
    private Disposable updateAllocateDisposable;
    private Disposable insertRuinDisposable;

    public void getGoods(String id, Consumer<Goods> success, Consumer<Exception> fail) {
//        BmobQuery<Category> bmobQuery = new BmobQuery<>();
//        bmobQuery.findObjects(new FindListener<Category>() {
//            @Override
//            public void done(List<Category> categories, BmobException e) {
//                if (e == null) {
//                    Snackbar.make(mBtnQuery, "查询成功：" + categories.size(), Snackbar.LENGTH_LONG).show();
//                } else {
//                    Log.e("BMOB", e.toString());
//                    Snackbar.make(mBtnQuery, e.getMessage(), Snackbar.LENGTH_LONG).show();
//                }
//            }
//        });
        Logger.e("iddsfasfas" + id);
        BmobQuery<Goods> bmobQuery = new BmobQuery<>();
        getGoodDisposable = bmobQuery.getObject(id, new QueryListener<Goods>() {
            @Override
            public void done(Goods good, BmobException e) {
                try {
                    if (e == null) {
                        Logger.d("查询成功：" + good.getName());
                        success.accept(good);
                    } else {
                        Logger.d(e.getMessage());
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void updateAllocateId(Goods goods, Consumer<Goods> success, Consumer<Exception> fail) {
        BmobQuery<AllocateGoods> query = new BmobQuery<>();

        String id = goods.getObjectId();
        query.addWhereContainsAll("goodsIds", Collections.singletonList(id));

        updateIdDisposable = query.findObjects(new FindListener<AllocateGoods>() {
            @Override
            public void done(List<AllocateGoods> allocateGoodsList, BmobException e) {
                if (e == null) {
                    updateGood(goods, success, fail);
                    updateAllocate(allocateGoodsList, id);
                } else {
                    Logger.d("find allocate fail");
                }
            }
        });
    }

    public void updateGood(Goods good, Consumer<Goods> success, Consumer<Exception> fail) {
        good.setRuin(true);
        updateGoodDisposable = good.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                try {
                    if (e == null) {
                        success.accept(good);
                    } else {
                        fail.accept(e);
                        Logger.d("find allocate fail");
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void updateAllocate(List<AllocateGoods> allocateGoodsList, String id) {
        AllocateGoods allocateGoods = allocateGoodsList.get(0);
        List<String> ids = new ArrayList<>(allocateGoods.getGoodsIds());
        ids.remove(id);

        allocateGoods.setGoodsIds(ids);
        allocateGoods.setNumber(allocateGoods.getNumber() - 1);
        allocateGoods.setHaveAllocate(allocateGoods.getHaveAllocate() + 1);

        allocateGoodsList.set(0, allocateGoods);

        List<String> ruinId = new ArrayList<>();
        ruinId.add(id);


        updateAllocateDisposable = allocateGoods.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Logger.d("updateAllocate 成功");
                } else {
                    Logger.d("失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });

        insertRuinGoods(allocateGoods, ruinId);

//        List<BmobObject> goods = new ArrayList<>(allocateGoodsList);
//        new BmobBatch().updateBatch(goods).doBatch(new QueryListListener<BatchResult>() {
//            @Override
//            public void done(List<BatchResult> results, BmobException e) {
//
//            }
//        });
    }

    public void insertRuinGoods(AllocateGoods allocateGoods, List<String> ids) {
        RuinGoods ruinGoods = new RuinGoods(allocateGoods.getName(), allocateGoods.getCompany()
                , allocateGoods.getAvatar(), allocateGoods.getPrice(), allocateGoods.getType(), ids.size(), ids, allocateGoods.getDepartments(), "");

        insertRuinDisposable = ruinGoods.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                try {
                    if (e == null) {
                        Logger.d("ruin good is saved");
                    } else {

                    }
                } catch (Exception e1) {

                }
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (getGoodDisposable != null) {
            getGoodDisposable.dispose();
            getGoodDisposable = null;
        }
        if (updateIdDisposable != null) {
            updateIdDisposable.dispose();
            updateIdDisposable = null;
        }
        if (updateGoodDisposable != null) {
            updateGoodDisposable.dispose();
            updateGoodDisposable = null;
        }
        if (updateAllocateDisposable != null) {
            updateAllocateDisposable.dispose();
            updateAllocateDisposable = null;
        }
        if (insertRuinDisposable != null) {
            insertRuinDisposable.dispose();
            insertRuinDisposable = null;
        }
    }
}
